package com.jysz.certificate.web.action.pm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.pm.PmUserStype;
import com.jysz.certificate.persistence.model.pm.PmUser;
import com.jysz.certificate.persistence.model.pm.PmUserList;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.certificate.service.pm.PmUserListService;
import com.jysz.certificate.service.pm.PmUserService;
import com.jysz.certificate.service.system.UserService;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/pm/user")
public class PmUserListAction extends AbstractBaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private PmUserService pmUserService;
	
	@Resource
	private PmUserListService pmUserListService;
	
	@Resource
	private UserService userService;
	
	private User us;
	private Long id;
	private Long pmId;
	private Long[] userId;
	private PmUser pmUser;
	private PmUser search;
	private PmUserList pmUserList;
	private boolean isClose;
	
	@Action("choose")
	public String choose() {
		if(!isClose){
			pmUserList = pmUserListService.find(pmId);
			setPage(userService.choose(us, getCurrentPage(), getPageSize()));	
		}
		return "choose";
	}
	
	@Action("edit")
	public String edit(){
		pmUser = pmUserService.find(id);
		setPage(pmUserListService.addUser(id, getCurrentPage(), getPageSize()));
		this.setRedirectUrl("index.do?currentPage=" + getCurrentPage(),"search",search);
		return ADD;
	}
	@Action("opPass")
	public String opPass(){
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
		PmUser pmUsers = pmUserService.find(id);
		pmUsers.setDeliceryMM(dft.format(new Date()));
		pmUsers.setDeliveryTime(new Date());
		pmUsers.setState(PmUserStype.SUBMITED);
		pmUserService.save(pmUsers);
		setSuccessPopMessage("名单提交成功");
		return SHOW_MSG_PAGE;
	}
	@Action("addUserList")
	public String addUserList(){
		PmUserList pmUserList = pmUserListService.find(pmId);
		pmUserList.setUsers(null);
		List<User> users = new ArrayList<User>();
		if(userId != null && userId.length>0){
			for(int i=0;i<userId.length;i++){
				User user =new User();
				user.setId(userId[i]);
				users.add(user);
			}
			pmUserList.setUsers(users);
		}else{
			pmUserList.setUsers(null);
		}
		
		pmUserListService.save(pmUserList);
		this.setRedirectUrl("choose.do?currentPage=" + getCurrentPage()+"&close=true&pmId="+pmId);
		return SHOW_MSG_PAGE;
	}
	@Action("openUpdate")
	public String openUpdate(){
		pmUserList = pmUserListService.find(pmId);
		return "";
	}


	public PmUser getPmUser() {
		return pmUser;
	}


	public void setPmUser(PmUser pmUser) {
		this.pmUser = pmUser;
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}

	public User getUs() {
		return us;
	}

	public PmUser getSearch() {
		return search;
	}

	public void setSearch(PmUser search) {
		this.search = search;
	}

	public void setUs(User us) {
		this.us = us;
	}

	public Long getPmId() {
		return pmId;
	}

	public void setPmId(Long pmId) {
		this.pmId = pmId;
	}

	public PmUserList getPmUserList() {
		return pmUserList;
	}

	public void setPmUserList(PmUserList pmUserList) {
		this.pmUserList = pmUserList;
	}

	public Long[] getUserId() {
		return userId;
	}

	public void setUserId(Long[] userId) {
		this.userId = userId;
	}

	public boolean isClose() {
		return isClose;
	}

	public void setClose(boolean isClose) {
		this.isClose = isClose;
	}
 

}
